package com.xhu.water_supply.service.impl;

import com.xhu.water_supply.entity.Administrator;
import com.xhu.water_supply.mapper.AdministratorMapper;
import com.xhu.water_supply.service.AdminInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class AdminInfoServiceImpl implements AdminInfoService {
    @Value("${file.server.path}")
    private String path;
    @Autowired
    private AdministratorMapper administratorMapper;

    /**
     * 查询全部管理员所有信息
     * @return
     */
    @Override
    public List<Administrator> getAllAdminInfo() {
        List<Administrator> administratorList = administratorMapper.selectAll();
        for(int i=0;i<administratorList.size();i++){
            if(administratorList.get(i).getAuthority().equals("a"))
                administratorList.get(i).setAuthority("超级管理员");
            else
                administratorList.get(i).setAuthority("普通管理员");
            administratorList.get(i).setPicture(path+administratorList.get(i).getPicture());
        }
        return administratorList;
    }

    /**
     * 模糊查询管理员所有信息
     * @return
     */
    @Override
    public List<Administrator> fuzzySearchAdminInfo(String searchStr,int select) {
        Example example=new Example(Administrator.class);
        example.selectProperties("id","name","account","password","authority","position","picture","date");
        switch (select){
            case 1:
                example.createCriteria().andLike("date","%"+searchStr+"%");
                break;
            case 2:
                example.createCriteria().andLike("id","%"+searchStr+"%");
                break;
            case 3:
                example.createCriteria().andLike("name","%"+searchStr+"%");
                break;
            case 4:
                example.createCriteria().andLike("account","%"+searchStr+"%");
                break;
            case 5:
                example.createCriteria().andLike("position","%"+searchStr+"%");
                break;
            case 6://不支持模糊查询
                String s="";
                if(searchStr.equals("超级管理员"))
                    s="a";
                else if(searchStr.equals("普通管理员"))
                    s="b";
                example.and().andEqualTo("authority",s);
                break;
        }
        List<Administrator> administratorList = administratorMapper.selectByExample(example);
        for(int i=0;i<administratorList.size();i++){
            if(administratorList.get(i).getAuthority().equals("a"))
                administratorList.get(i).setAuthority("超级管理员");
            else
                administratorList.get(i).setAuthority("普通管理员");
        }
        return administratorList;
    }

    /**
     * 通过账户查询所有信息
     * @param account
     * @return
     */
    @Override
    public Administrator getByAccount(String account) {
        Example example = new Example(Administrator.class);
        example.selectProperties("id","name","account","password","authority","position","picture","date");
        example.and().andEqualTo("account", account);
        return administratorMapper.selectOneByExample(example);
    }

    /**
     * 通过id查询名称、账户、密码、职位、权限、照片
     * @param id
     * @return
     */
    @Override
    public Administrator getById(String id) {
        Example example = new Example(Administrator.class);
        example.selectProperties("id","name","account","password","authority","position","picture","date");
        example.and().andEqualTo("id", id);
        Administrator administrator = administratorMapper.selectOneByExample(example);
        if(administrator.getAuthority().equals("a"))
            administrator.setAuthority("超级管理员");
        else
            administrator.setAuthority("普通管理员");
        administrator.setPicture(path+administrator.getPicture());
        return administrator;
    }

    /**
     * 通过账户修改信息
     * @param administrator
     * @return
     */
    @Override
    public int modifyAdminInfoByAccount(Administrator administrator) {
        Example example = new Example(Administrator.class);
        example.createCriteria().andEqualTo("account", administrator.getAccount());
        return administratorMapper.updateByExampleSelective(administrator, example);
    }

    /**
     * 通过Id修改信息
     * @param administrator
     * @return
     */
    @Override
    public int modifyAdminInfoById(Administrator administrator) {
        Example example = new Example(Administrator.class);
        example.createCriteria().andEqualTo("id", administrator.getId());
        if(administrator.getAuthority().equals("超级管理员"))
            administrator.setAuthority("a");
        else
            administrator.setAuthority("b");
        return administratorMapper.updateByExampleSelective(administrator, example);
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @Override
    public int deleteAdminInfoById(String id) {
        int a=administratorMapper.deleteByPrimaryKey(id);
        return a;
    }

    /**
     * 添加管理员信息
     * @param administrator
     * @return
     */
    @Override
    public int addAdminInfo(Administrator administrator) {
        int a=administratorMapper.insert(administrator);
        return a;
    }
}
